package com.zhaogang.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.zhaogang.entity.SaleAccount;
import com.zhaogang.entity.UserAccount;

public class LoginInterceptor implements HandlerInterceptor {

	public LoginInterceptor() {
		super();
	}

	// 日志
	// protected Logger log = Logger.getLogger(getClass());
	/**
	 * Handler执行之前调用这个方法
	 */
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		// 创建session
		HttpSession session = request.getSession();

		// 无需登录，允许访问的地址 :商城首页，用户商户登陆界面，管理员登陆界面
		String[] allowUrls = new String[] { "goods/firstQuery", "saleAccount/loginValidate", "admin/toLogin",
				"admin/login", "goods/specialShow","saleAccount/registValidate", "goods/specialQuery", "goods/detail", "goods/buy" };
		/*
		 * 通过instanceof关键字来判断session的类型 用户账户为UserAccount对象 商户账户是SaleAccount对象
		 * 管理员账户是String类型的值
		 */
		UserAccount userAccount = (UserAccount) session.getAttribute("userToken");
		SaleAccount saleAccount = (SaleAccount) session.getAttribute("saleToken");
		String adminAccount = (String) session.getAttribute("loginName");

		if (session.getAttribute("userToken") instanceof UserAccount) {
			userAccount = (UserAccount) session.getAttribute("userToken");
		} else if (session.getAttribute("saleAccount") instanceof SaleAccount) {
			saleAccount = (SaleAccount) session.getAttribute("saleAccount");
		} else {
			adminAccount = (String) session.getAttribute("loginName");
		}
		// 获取请求地址
		String url = request.getRequestURL().toString();
		for (String strUrl : allowUrls) {
			if (url.contains(strUrl)) {
				return true;
			}
		}
		//用户拦截控制
		if (saleAccount != null) {
			return true;
			//商户拦截控制
		}else if(userAccount != null) {
			System.out.println("用户账户session拦截控制");
			String[] notAllowUrls = new String[] { "index", "goods/goods", "babyAccount/newQuery",
					"babyAccount/newInsert", "babyAccount/toUpdate", "babyAccount/updateBaby", "babyAccount/toDelete",
					"babyAccount/toNewInsert", "goods/toAdd", "goods/insert", "goods/toUpdate", "goods/update",
					"goods/delete", "goods/queryByCon", "sale/infoShow", "saleOrder/saleOrderList",
					"saleInfo/modifyInfo", "admin/showUser", "admin/showOrder", "admin/showSale", "log/log",
					"admin/list" };
			for (String strUr2 : notAllowUrls) {
				if (url.contains(strUr2)) {
					// Session里没有值(未登录)，不符合条件，跳转到登录界面
					JOptionPane.showMessageDialog(null, "您未有访问权限", "请登录商户账号", JOptionPane.ERROR_MESSAGE);
					request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
					// System.out.println("session无记录，代表未登录，跳转登录页面");
					return false;
				}
			}
			return true;
		//管理员拦截控制
			}else if (adminAccount != null) {
			System.out.println("商户账户session拦截控制");
			String[] notAllowUrls1 = new String[] { "index", "goods/goods", "babyAccount/newQuery",
					"babyAccount/newInsert", "babyAccount/toUpdate", "babyAccount/updateBaby", "babyAccount/toDelete",
					"babyAccount/toNewInsert", "goods/toAdd", "goods/insert", "goods/toUpdate", "goods/update",
					"goods/delete", "goods/queryByCon", "sale/infoShow", "saleOrder/saleOrderList",
					"saleInfo/modifyInfo", "userInfo/getUserInfo" };
			for (String strUr2 : notAllowUrls1) {
				if (url.contains(strUr2)) {
					System.out.println("走到这里了没？");
					// Session里没有值(未登录)，不符合条件，跳转到登录界面
					JOptionPane.showMessageDialog(null, "您未有访问权限", "请登录商户账号", JOptionPane.ERROR_MESSAGE);
					request.getRequestDispatcher("/pages/admin/adminLogin.jsp").forward(request, response);
					// System.out.println("session无记录，代表未登录，跳转登录页面");
					return false;
				}
				return true;
			}
		} else
			// Session里没有值(未登录)，不符合条件，跳转到登录界面
			request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
		// System.out.println("session无记录，代表未登录，跳转登录页面");
				return false;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

}
